﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Ec.NETCoreFramework.Common.Core
{
    /// <summary>
    /// 框架响应
    /// </summary>
    public class FrameworkResponse<T>:IResponse<T>
    {
        /// <summary>
        /// /
        /// </summary>
        public string Code { get; private set; }
        /// <summary>
        /// /
        /// </summary>
        public string Message { get; private set; }
        /// <summary>
        /// /
        /// </summary>
        public string ErrDetail { get; private set; }
        /// <summary>
        /// /
        /// </summary>
        public T Content { get; private set; }
        
        /// <summary>
        /// 
        /// </summary>
        public bool Success { get; set; }
        /// <summary>
        /// 成功
        /// </summary>
        /// <param name="_Content"></param>
        /// <param name="msg"></param>
        /// <returns></returns>
        public FrameworkResponse<T> OK(T _Content, string msg = null)
        {
            Code = EnumReturnCode.Success.GetHashCode().ToString();
            Message = msg;
            Content = _Content;
            Success = true;
            return this;
        }
        /// <summary>
        /// 失败
        /// </summary>
        /// <param name="enum"></param>
        /// <param name="msg"></param>
        /// <param name="_Content"></param>
        /// <returns></returns>
        public FrameworkResponse<T> Fail(EnumReturnCode @enum, string msg = null, T _Content = default(T),string code=null)
        {
            Code = code != null ? code : @enum.GetHashCode().ToString();
            Message = msg;
            ErrDetail = msg;
            Content = _Content;
            Success = false;
            return this;
        }
    }

    /// <summary>
    /// 
    /// </summary>
    public static partial class FrameworkResponse
    {

        /// <summary>
        /// 成功
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="_Content"></param>
        /// <param name="msg"></param>
        /// <returns></returns>
        public static IResponse Success<T>(T _Content = default(T), string msg = null)
        {
            return new FrameworkResponse<T>().OK(_Content, msg);
        }

        /// <summary>
        /// 成功
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="_Content"></param>
        /// <param name="msg"></param>
        /// <returns></returns>
        public static IResponse<T> SuccessData<T>(T _Content = default(T), string msg = null)
        {
            return new FrameworkResponse<T>().OK(_Content, msg);
        }

        /// <summary>
        /// 成功
        /// </summary>
        /// <returns></returns>
        public static IResponse Success()
        {
            return Success<string>();
        }
        /// <summary>
        /// 参数缺失
        /// </summary>
        /// <param name="msg"></param>
        /// <returns></returns>
        public static IResponse MissParam(string msg = null)
        {

            return new FrameworkResponse<string>().Fail(EnumReturnCode.MissParam, msg);
        }

        /// <summary>
        /// 系统错误
        /// </summary>
        /// <param name="msg"></param>
        /// <returns></returns>
        public static IResponse SystemError(string msg = null)
        {

            return new FrameworkResponse<string>().Fail(EnumReturnCode.SystemError, msg);
        }

        /// <summary>
        /// 系统错误带返回
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="_Content"></param>
        /// <param name="msg"></param>
        /// <returns></returns>
        public static IResponse SystemErrorData<T>(T _Content = default(T), string msg = null,string code=null)
        {

            return new FrameworkResponse<T>().Fail(EnumReturnCode.SystemError, msg, _Content, code);
        }

        /// <summary>
        /// 未知错误
        /// </summary>
        /// <param name="msg"></param>
        /// <returns></returns>
        public static IResponse UnkonwError(string msg = null)
        {

            return new FrameworkResponse<string>().Fail(EnumReturnCode.UnknowError, msg);
        }

        

    }
}
